package cn.zyl.functions;


import android.os.SystemClock;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.SimpleFormatter;

/**
 * 计时器
 * Created by weili0677 on 2016/8/4.
 */
public class StopWatch {
    SimpleDateFormat date_mmss = new SimpleDateFormat("mm:ss", Locale.CHINA);
    Timer timer;
    long base=0;
    long time=0;
    long maxTime=0;
    int state = 0;
    int minute=0;
    int second=0;

    /**
     * 构建函数
     * @param minute  多少分钟，最多60分钟
     * @param second  多少秒,最多60秒
     */
    public StopWatch(int minute,int second) {
        this.minute = minute;
        this.second = second;
        maxTime = this.minute*60*1000+this.second*1000;
    }

    public void start(){
        timer = new Timer();
        base = SystemClock.elapsedRealtime();
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                time = SystemClock.elapsedRealtime()-base;
                if (time>=maxTime){
                    stop();
                    reset();
                }
            }
        };
        timer.schedule(timerTask,0,1);
        state = 1;  //正在运行
    }

    public void stop(){
        timer.cancel();
        state = 2;  //暂停
    }

    public void reset(){
        time=0;
        state = 0;  //未运行
    }

    public long getTime(){
        return time;
    }

    public int getState(){
        return state;
    }

    public String getTimeText(String format){
//        s_format = "{60m}:{00s}:{ms}";
        //这里先不管有多少秒吧
        long mills = time % (1000);
        long second = time % (60*1000) / 1000;  //这个最多显示60秒
        long minute = time / (60*1000);  //直接除60秒,这个可以显示比如100分钟吧
        String s_mills = mills+"";
        String s_second = second<10?"0"+second:""+second;
        String s_minute = minute<10?"0"+minute:""+minute;
        String result = format.replace("{ms}",s_mills);
        if (result.contains("s}")){
            String temp = format.substring(0,format.indexOf("s}")+2);
            temp = temp.substring(temp.lastIndexOf("{"),temp.length());
            result = result.replace(temp,s_second);
        }
        if (result.contains("m}")){
            String temp = format.substring(0,format.indexOf("m}")+2);
            temp = temp.substring(temp.lastIndexOf("{"),temp.length());
            result = result.replace(temp,s_minute);
        }
        return result;
    }

    public long getMaxTime(){
        return maxTime;
    }

    public void setMinute(int minute) {
        this.minute = minute;
        maxTime = this.minute*60*1000+this.second*1000;
    }

    public void setSecond(int second) {
        this.second = second;
        maxTime = this.minute*60*1000+this.second*1000;
    }

    /**
     * run角度-秒
     * @return
     */
    public float getSecondSweepAngle()
    {
        long mills = time % (this.second*1000);
        return mills*360 / (this.second*1000F);
    }

    /**
     * walk角度-秒
     * @return
     */
    public float getSecondWalkAngle()
    {
        long mills = time % (this.second*1000);
        long second = mills / 1000;
        return second * 360 / (this.second*1f);
    }

    /**
     * run角度-分
     * @return
     */
    public float getMinuteSweepAngle()
    {
        long mills = time % (this.minute*60*1000);
        return mills*360 / (this.minute*60*1000F);
    }

    /**
     * walk角度-分
     * @return
     */
    public float getMinuteWalkAngle()
    {
        long mills = time % (this.minute*60*1000);
        long minute = mills / 60*1000;
        return minute * 360 / (this.minute*1f);
    }
}
